package com.liusand.controller;

import com.liusand.common.Result;
import com.liusand.dto.UpdateOrderDto;
import com.liusand.pojo.PageInfo;
import com.liusand.service.OrderService;
import com.liusand.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.controller
 * fileName :OrderController
 * creationTime :2022/11/30 15:47
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("page")
    public Result page(String beginTime,
                       String endTime,
                       String number,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime begin = beginTime != null ? LocalDateTime.parse(beginTime, dateTimeFormatter) : null;
        LocalDateTime end = endTime != null ? LocalDateTime.parse(endTime, dateTimeFormatter) : null;
        PageInfo<OrderVo> info = orderService.page(begin, end, number, page, pageSize);
        return Result.success(info);
    }

    @PutMapping
    public Result modifyOrderStatus(@RequestBody UpdateOrderDto updateOrderDto){
        if(updateOrderDto == null){
            return Result.error("修改订单状态失败");
        }
        boolean modifyResult = orderService.modifyOrderStatus(updateOrderDto);
        if(!modifyResult){
            return Result.error("修改订单状态失败");
        }
        return Result.success("修改订单状态成功");
    }
}
